package com.card.advice;

import java.util.Date;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.xiaoao.net.db.DBManager;
import com.xiaoao.net.db.Entity;
import com.xiaoao.net.utils.PrimaryKey;
import com.xiaoao.net.utils.PubFunc;
import com.xiaoao.net.utils.TimeUtils;
import com.xiaoao.net.utils.json.GSONUtils;

public class Advice{
	//邮件属性值
	public static final String ADVICEID = "adviceID";
	public static final String USERID = "userID";
	public static final String CONTENT = "content";
	public static final String SUBMITDATE="submitDate";
	public static final String STATE="state";//状态
	
	
	private String adviceID;//反馈id
	private String userID;//用户id
	private String content;//用户反馈内容
	private Date submitDate;//提交反馈时间
	private int state;//反馈内容状态
	public Advice(String userID,String content,Date submitDate,int state) {
		this.adviceID = PrimaryKey.getPrimaryKey("c5Advice");
		this.userID=userID;
		this.content=content;
		this.submitDate=submitDate;
		this.state=state;
	}
	
	public Advice(Entity entity){
		this.adviceID = getStringProperty(ADVICEID, entity);
		this.userID = getStringProperty(USERID, entity);
		this.content=getStringProperty(CONTENT, entity);
		this.submitDate= new Date(getLongProperty(SUBMITDATE, entity));
		this.state=getIntProperty(STATE,entity);
	}
	
	public int getIntProperty(String name, Entity entity) {
		return (int)entity.getProperty(name);
	}
	
	public String getStringProperty(String name, Entity entity) {
		return PubFunc.toString(entity.getProperty(name));
	}
	
	public long getLongProperty(String name, Entity entity) {
		return (long)entity.getProperty(name);
	}
	
	public void setAdviceId(String adviceID) {
		this.adviceID = adviceID;
	}
 
	public void setUserID(String userID) {
		this.userID = userID;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
	public void setSubmitDate(Date submitDate) {
		this.submitDate = submitDate;
	}
	
	public void setState(int state) {
		this.state = state;
	}
	
	public void saveDb() {
		Gson gson = new GsonBuilder()
				.setDateFormat(GSONUtils.DATEFORMAT)
				.disableHtmlEscaping()
				.create();
		
		Entity entity = new Entity(AdviceManager.T_ADVICE_DATA,adviceID);
		entity.setProperty(Advice.ADVICEID, adviceID);
		entity.setProperty(Advice.USERID, userID);
		entity.setProperty(Advice.CONTENT, content);
		entity.setProperty(Advice.SUBMITDATE, submitDate.getTime());
		entity.setProperty(Advice.STATE, state);
		
		DBManager.getInstance().saveOrUpdate(entity);
	}

	public String getAdviceId() {
		return adviceID;
	}

	public String getUserID() {
		return userID;
	}
	
	public String getContent() {
		return content;
	}
	
	public String getSubmitDate() {
		return TimeUtils.ToString(submitDate);
	}
	
	public int getState() {
		return state;
	}
	

	@Override
	public String toString() {
		return "Advice [adviceID=" + adviceID + ", userID=" + userID + ", content=" + content + ", submitDate="
				+ submitDate + ", state=" + state + "]";
	}
	
	
}
